﻿using System;
using System.Collections.Generic;
using System.Text;
using Hime.Redist.Parsers;

namespace xowl.gmi.compiler.ast
{
    enum ParameterValueType
    {
        ValueInteger, ValueString, ValueColor, Parameter
    }

    class Parameter
    {
        private ParameterValueType type;
        private string name;
        private string value;

        public ParameterValueType ValueType { get { return type; } }
        public string Name { get { return name; } }
        public string Value { get { return value; } }
        public string ValueOWL { get { return GetOWLRepresentation(); } }
        
        public Parameter(view.Parameter origin, view.Value value)
        {
            this.name = origin.Name;
            this.value = value.ToString();
            if (value is view.ConstantInteger) type = ParameterValueType.ValueInteger;
            if (value is view.ConstantString) type = ParameterValueType.ValueString;
            if (value is view.ConstantColor) type = ParameterValueType.ValueColor;
            if (value is view.Parameter) type = ParameterValueType.Parameter;
        }
        public Parameter(SyntaxTreeNode ast)
        {
            this.name = (ast.Children[0].Symbol as SymbolTokenText).ValueText;
            this.value = (ast.Children[1].Symbol as SymbolTokenText).ValueText;
            switch (ast.Children[1].Symbol.Name)
            {
                case "PARAMETER": type = ParameterValueType.Parameter; break;
                case "INTEGER": type = ParameterValueType.ValueInteger; break;
                case "COLOR": type = ParameterValueType.ValueColor; break;
                case "STRING": type = ParameterValueType.ValueString; break;
            }
        }

        public void Write(System.IO.StreamWriter writer)
        {
            writer.Write(name);
            writer.Write("=");
            writer.Write(value);
        }
        public void WriteOWL(System.IO.StreamWriter writer, string id)
        {
            if (type == ParameterValueType.Parameter)
                return;
            writer.WriteLine("\t\t\tMeta(DataPropertyAssertion(view:" + name + " " + id + " " + GetOWLRepresentation() + "))");
        }
        private string GetOWLRepresentation()
        {
            if (type == ParameterValueType.ValueInteger) return value;
            if (type == ParameterValueType.ValueString) return value;
            if (type == ParameterValueType.ValueColor) return Int32.Parse(value.Substring(1), System.Globalization.NumberStyles.HexNumber).ToString();
            return null;
        }
    }
}
